﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db;
using eCommerce.db.BORespository;
using eCommerce.db.Cart;
using eCommerce.db.DataRespository;

namespace eCommerce.web.Controls.Product
{
    public partial class LargeCart : System.Web.UI.UserControl
    {
        protected int CustomerId;
        protected CustomerEntity Customer;

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (HttpContext.Current.Session["eCommerce.customer"] != null)
            {
                Customer    = (CustomerEntity)Session["eCommerce.customer"];
                CustomerId  = Customer.CustomerId;
            }

            if (!IsPostBack)
                ListCart();
        }
        protected void ListCart()
        {
            rptCart.DataSource = ShoppingCart.Instance.Items;
            rptCart.DataBind();
            ltTotalCart.Text = Utils.FormatPrice(ShoppingCart.Instance.GetSubTotal());
        }
        
        protected void BtnCheckOut(object sender, EventArgs e)
        {
            
        }

        protected void rptCart_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltImage     = (Literal)e.Item.FindControl("ltImage");
                var ltName      = (Literal)e.Item.FindControl("ltName");
                var ltTotalItem = (Literal)e.Item.FindControl("ltTotalItem");

                var productId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfProduct")).Value);
                var quantity  = Utils.ToInt32(((TextBox)e.Item.FindControl("txtQuantity")).Text);

                var o = ProductBo.GetById(productId);
                if (o != null)
                {
                    ltImage.Text = string.Format(Constant.ThumbImage, productId, (int)SettingType.Product, Utils.GetSetting("ThumbSize60", SettingType.Int32));
                    ltName.Text = string.Format("<a href=\"{0}\">{1}</a>",
                                        UrlHelper.ProductDetail(CategoryBo.GetCategoryName(o.CategoryId), o.ProductId, o.Name), o.Code); //o.Code + "-" + o.Name

                    if (productId > 0 && quantity > 0)
                        ltTotalItem.Text = Utils.FormatPrice(Utils.ToDecimal(o.UnitPrice * quantity));
                }
            }
        }

        protected void rptCart_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                var productId = Convert.ToInt32(e.CommandArgument);
                ShoppingCart.Instance.RemoveItem(productId);
            }
            else if (e.CommandName == "UpdateCart")
            {
                foreach (RepeaterItem row in rptCart.Items)
                {
                    if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                    {
                        var productId = Utils.ToInt32(((HiddenField)row.FindControl("hfProduct")).Value);
                        var quantity  = Utils.ToInt32(((TextBox)row.FindControl("txtQuantity")).Text);

                        if (productId > 0 && quantity > 0)
                            ShoppingCart.Instance.SetItemQuantity(productId, quantity);
                    }
                }
            }            

            ListCart();
        }    
    }
}